package com.cx.controller.czw;

import com.cx.pojo.User;
import com.cx.service.czw.QiniuService;
import com.cx.service.czw.UserInfoService;
import com.cx.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
@RequestMapping("userInfo")
@MultipartConfig
public class UserInfoController {

    @Autowired
    UserInfoService userInfoService;
    @Autowired
    private QiniuService qiniuService;

    //根据登录/注册的电话查询用户信息
    @RequestMapping("selectUserInfo")
    @ResponseBody
    public ResponseResult selectUserInfo(HttpSession session){
        String phone = String.valueOf(session.getAttribute("phone"));
        ResponseResult responseResult = new ResponseResult();
        User user = userInfoService.selectUserInfo(phone);
        if(null == user){
            responseResult.setMessage("显示信息失败");
            responseResult.setCode(2);
        }else {
            responseResult.setMessage("显示信息成功");
            responseResult.setCode(1);
            responseResult.setData(user);
        }
        return responseResult;
    }

    //根据用户的信息，电话。来修改用户信息
    @RequestMapping("updateUserInfo")
    @ResponseBody
    public ResponseResult updateUserInfo( MultipartFile file ,User user, HttpSession session){
        System.out.println("user=" +user);
        String phone = String.valueOf(session.getAttribute("phone"));
        ResponseResult responseResult = new ResponseResult();
        System.out.println(phone);
        System.out.println(session.getAttribute("phone"));
        System.out.println("file=" +file);
        if(null == file) {
            responseResult.setMessage("头像为空");
            responseResult.setCode(3);
            return responseResult;
        }
        try {
            String fileUrl = qiniuService.saveImage(file);
            user.setHeadPortrait(fileUrl);
            System.out.println("fileUrl =" + fileUrl);
        } catch (IOException e) {
            e.printStackTrace();
        }
        int affectRows = userInfoService.updateUserInfo(phone , user);
        System.out.println(affectRows);
        if(0 != affectRows){
            responseResult.setMessage("信息修改成功");
            responseResult.setCode(1);
        }else {
            responseResult.setMessage("信息修改失败");
            responseResult.setCode(2);
        }
        return responseResult;
    }

    //根据用户登录状态 右上角信息
    @RequestMapping("loginStatus")
    @ResponseBody
    public ResponseResult loginStatus(HttpSession session){
        ResponseResult responseResult = new ResponseResult();
        Object login = session.getAttribute("login");
        if(null != login){
            responseResult.setMessage("登录状态");
            responseResult.setCode(1);
            responseResult.setData(session.getAttribute("phone"));
        }else {
            responseResult.setMessage("非登录状态");
            responseResult.setCode(2);
        }
        return responseResult;
    }

    //登录后，如果用户名字不为空，显示姓名，为空则显示电话
    @RequestMapping("loginName")
    @ResponseBody
    public ResponseResult loginName(HttpSession session){
        ResponseResult responseResult = new ResponseResult();
        String phone = String.valueOf(session.getAttribute("phone"));
        User user = userInfoService.selectUserInfo(phone);
        String userName = user.getUserName();
        if(null != userName){
            responseResult.setMessage("有昵称！");
            responseResult.setCode(1);
            responseResult.setData(userName);
        }else {
            responseResult.setMessage("未设置昵称！");
            responseResult.setCode(2);
            responseResult.setData(phone);
        }
        return responseResult;
    }

    //点击退出之后 清除一切session
    @RequestMapping("exit")
    @ResponseBody
    public ResponseResult exit(HttpSession session){
        ResponseResult responseResult = new ResponseResult();
          session.removeAttribute("phone");
          session.removeAttribute("code");
          session.removeAttribute("login");
          session.removeAttribute("randomPhoneCode");
//          session.invalidate();
        if(null != session.getAttribute("login")){
            responseResult.setMessage("退出失败！");
            responseResult.setCode(2);
        }else {
            responseResult.setMessage("退出成功");
            responseResult.setCode(1);
        }
        return responseResult;
    }
}
